/**
 * Copyright (c) 2018, dreamkaylee@foxmail.com All Rights Reserved.
 */
  
package configuration;

import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
 * 事务配置
 * @author limk
 * @date 2018年2月14日 下午4:49:45
 * @version 1.0
 */
@Configuration
public class TransactionConfig {
	
	private static final Logger LOGGER = LogManager.getLogger(TransactionConfig.class);
	
	@Autowired
    private DataSourceTransactionManager transactionManager;
	
	@Bean(name = "txAdvice")
    public TransactionInterceptor getAdvisor() throws Exception {
		LOGGER.info("----------配置事务----------");
        Properties properties = new Properties();
        properties.setProperty("add*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("save*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("edit*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception");
        
        properties.setProperty("get*", "PROPAGATION_REQUIRED,readOnly");
        properties.setProperty("find*", "PROPAGATION_REQUIRED,readOnly");
        properties.setProperty("select*", "PROPAGATION_REQUIRED,readOnly");
        properties.setProperty("*", "PROPAGATION_REQUIRED");
 
        TransactionInterceptor interceptor = new TransactionInterceptor(transactionManager, properties);
        return interceptor;
    }
 
    @Bean
    public BeanNameAutoProxyCreator txProxy() {
    	LOGGER.info("----------开启事务扫描----------");
        BeanNameAutoProxyCreator proxycreate = new BeanNameAutoProxyCreator();
        proxycreate.setInterceptorNames("txAdvice");
        proxycreate.setBeanNames("*Service", "*ServiceImpl");
        proxycreate.setProxyTargetClass(true);
        return proxycreate;
    }
	
}
  